import { Component, ViewEncapsulation, ViewChild, OnInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { HttpService } from '../../service/http.service';

@Component({
  selector: 'app-scheduling',
  templateUrl: './scheduling.component.html',
  styleUrls: ['./scheduling.component.css'],
  encapsulation: ViewEncapsulation.None
})

export class SchedulingComponent implements OnInit {

  //获取排班信息所需的科室id
  deptid: number;
  //获取日期接口
  dateurl = 'public!getSysDayAndWeekInfo';
  // dateurl = 'assets/mock/date.json';
  //获取日期所接收的变量
  datelist: any;
  //日期分段渲染的变量
  schedulingdate: any;
  datearry: any[] = [];
  dateindexi: number;
  dateindexj: number;
  //获取排班的接口
  schedulingurl = 'hisinfo!getArrangementInfo';
  // schedulingurl = 'assets/mock/schedule.json';
  //获取排班所接收的变量
  schedulinglist: any;
  page = 1;
  cho2 = true;
  cho1: number;
  first: boolean;
  chenge: boolean;

  constructor(private routerdata: ActivatedRoute, private listdata: HttpService, private router: Router) { }

  ngOnInit() {
    this.routerdata.params.subscribe((params: Params) => { this.deptid = params['id']; });
    this.getSysTime();
  }

  //获取顶部的日期
  getSysTime() {
    this.listdata.httpPost(this.dateurl, {}).subscribe(res => {
      this.datelist = res.data.days;
      this.getSchedulingList(this.datelist[0].date, 0, 0);
      let l = 0;
      this.cho1 = Math.ceil(this.datelist.length / 4) - 1;
      for (let g = 0; g < this.datelist.length / 4; g++) {
        const b = [];
        for (let j = 0; j < 4; j++) {
          if (this.datelist[l] != null) {
            b.push(this.datelist[l]);
          } else {
            b.push([]);
          }
          l++;
        }
        this.datearry.push(b);
      }
      this.schedulingdate = this.datearry;
    })
  }
  //获取医生排班数据
  getSchedulingList(time, i, j) {
    this.chenge = false;
    if (time != undefined) {
      this.dateindexi = i;
      this.dateindexj = j;
      let data = "{RQ:" + time + ",KSID:" + this.deptid + ",YSID:'',YSXM:'',HZDW:'掌上医疗'}";
      this.listdata.httpPost(this.schedulingurl, data).subscribe(res => {
        if (this.listdata.dataFilter(res)) {
          this.schedulinglist = res.data.HBLIST.HB;
          this.first = true;
          this.chenge = true;
          sessionStorage.setItem('schedulingdate', JSON.stringify(this.schedulingdate[i][j]));
          console.log('接口名称:' + this.schedulingurl, this.schedulinglist);
        }
      });
    }
  }

  doctorIndex() {
    this.router.navigate(['/doctorhome']);
  }
  //上一页
  previous(index: number) {
    this.page = index;
  }
  //下一页
  next(index: number) {
    this.page = index;
  }
  //进入挂号信息确认界面
  doRegister(i) {
    sessionStorage.setItem('scheduling', JSON.stringify(this.schedulinglist[i]))
    this.router.navigate(['/appoint/contrement']);
  }
}
